/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bllRoom;

import bllInterface.BLLChatServer;
import bllInterface.iRoom;
import bllInterface.RoomChatManager;
import bllInterface.iRoomListener;
import dalfactory.SDALFactory;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.DataRow;
import utils.DataTable;

/**
 *
 * @author Mrkupi
 */
public class RoomChatManagerImpl implements RoomChatManager
{
    private Map<String, iRoom> roomDict = new HashMap();
    private ArrayList<iRoomListener> listeners = new ArrayList<>();
    private BLLChatServer chatSv;
    
    public RoomChatManagerImpl(BLLChatServer chatSv)
    {
        this.chatSv = chatSv;
        
        loadDatabaseRoom();
    }

    private void loadDatabaseRoom()
    {
        try 
        {
            DataTable dt = SDALFactory.getInstance().getRoom().getAllRoom();
            if(dt != null && dt.size() != 0)
            {
                for(int i = 0; i < dt.size(); ++i)
                {
                    DataRow row     =   dt.get( i );
                    String roomId   =   row.get( "RoomID" );
                    String roomName =   row.get( "Name" );
                    
                    iRoom room = new RoomImpl(Integer.parseInt(roomId), roomName, chatSv);
                    chatSv.login(room);

                    this.roomDict.put(room.getChatID(), room);

                    try
                    {
                        notifyAll( Integer.parseInt(roomId), roomName );
                    }
                    catch(RemoteException ex)
                    {
                        // WTF of error ???
                    }
                }
            }
        }
        catch (RemoteException ex) 
        {
            Logger.getLogger(RoomChatManagerImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public String createRoomChat( String roomName ) throws RemoteException
    {
        int id = SDALFactory.getInstance().getRoom().nextRoomID();
        if (id != -1)
        {
            iRoom room = new RoomImpl(id, roomName, chatSv);
            chatSv.login(room);
            
            this.roomDict.put(room.getChatID(), room);
            
            notifyAll(id, roomName);
            
            // Register room to database
            SDALFactory.getInstance().getRoom().insertRoom(id, roomName);
            
            return String.valueOf(id);
        }
        
        // NEED TABLE
        return "Error";
    }
    
    private void notifyAll(int id ,String roomName) throws RemoteException{
        for(int i = 0; i < listeners.size(); ++i)
        {
            listeners.get(i).add(String.valueOf(id) + ":" + roomName);
        }
    }

    @Override
    public iRoom getRoom(String id) throws RemoteException
    {
        return this.roomDict.get(id);
    }

    @Override
    public ArrayList<String> getAllRoom() throws RemoteException 
    {
        ArrayList<String> rooms = new ArrayList<>();
        
        DataTable dt = SDALFactory.getInstance().getRoom().getAllRoom();
        if(!dt.isEmpty())
        {
            for(int i = 0; i < dt.size(); ++i)
            {
                DataRow row = dt.get(i);
                String roomID = row.get("RoomID");
                String roomName = row.get("Name");
                
                if(!roomDict.containsKey(roomID))
                {
                    iRoom room = new RoomImpl(Integer.parseInt(roomID), roomName, chatSv);
                }
                rooms.add(roomID);
            }
        }
        
        return rooms;
    }

    @Override
    public void addRoomListener(iRoomListener listener) throws RemoteException 
    {
        for(String key : roomDict.keySet())
        {
            iRoom room = roomDict.get(key);
            listener.add(room.getChatID() + ":" + room.getName());
        }
        
        listeners.add(listener);
    }

    @Override
    public void removeRoomListener(iRoomListener listener) throws RemoteException 
    {
        listeners.remove(listener);
    }
}
